<?php


namespace app\api\controller;

use app\api\controller\room\Room;
use app\api\model\MeetModel;
use app\api\model\MeetUserModel;
use app\api\model\room\RoomModel;
use app\api\model\room\RoomOrderModel;
use app\api\model\room\RoomTimesModel;
use app\api\model\room\RoomWeekModel;
use app\common\controller\Api;
use think\Db;
use think\Response;

class MeetRoom extends Api
{
// 无需登录的接口,*表示全部
    protected $noNeedLogin = ['info'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // 列表
    public function lists()
    {
        $day = input('date') ? input('date') : date("Y-m-d");

        $year       = $this->request->param('year', date('Y'));
        $month      = $this->request->param('month', date('m'));
        $day        = $this->request->param('day', date('d'));
        $edifice_id = $this->request->param('edifice_id');
        $week       = date('w', strtotime($year . '-' . $month . '-' . $day));
        // 查询时间
        $id_arr = RoomWeekModel::where('week', $week)->select();
        // 获取房间列表
        $rooms = RoomModel::where('id', 'in', array_column($id_arr, 'room_id'))
            ->field("*")
            ->select();
        foreach ($rooms as &$value) {
            /*
          // 查询房间预订情况
          $orders      = RoomOrderModel::where('room_id', $value['id'])->where('status', 'neq', -1)->where('starttime', '>', time() - 86400)->field('starttime,hour')->select();
          $order_times = [];
          foreach ($orders as $order) {
              //会议结束时间
              $endtime = $order['starttime'] + $order['hour'] * 3600;
              for ($i = 0; $i <= $order['hour']; $i++) {
                  //当前小时开始时间
                  $hourtime = strtotime(date('Y-m-d H:00:00', $order['starttime'] + $i * 3600));
                  if ($i == 0) {
                      $b = $endtime > ($hourtime + 3600) ? ($hourtime + 3600 - $order['starttime']) / 3600 : $order['hour'];
                  } else {
                      $b = ($endtime - $hourtime) > 3600 ? 1 : ($endtime - $hourtime) / 3600;
                  }

                  $key = date('n-j-G', $hourtime);
                  // 两个时间段在同一小时内交叉时需要相加
                  if (!empty($order_times[$key])) {
                      $order_times[$key] += round($b * 100, 2);
                  } else {
                      $order_times[$key] = round($b * 100, 2);
                  }

              }
          }

          // 获取开放时间
          $times  = RoomTimesModel::field('time,time_stage')->where('room_id', $value['id'])->select();
          // echo $this->roomTimes->getLastSql();
          $_times = [];
          foreach ($times as $time) {
              $key                   = $month . '-' . $day . '-' . $time['time'];
              $_times[$time['time']][] = [$time['time_stage']=>0];
          }

          foreach ($_times as $key => $time){
              $tempTime = array();
              $tempTime["1"] = 1;
              $tempTime["2"] = 1;
              $tempTime["3"] = 1;
              $tempTime["4"] = 1;
              if (key_exists("1",$_times[$key])){
                  $tempTime["1"] = 0;
              }
              if (key_exists("2",$_times[$key])){
                  $tempTime["2"] = 0;
              }
              if (!key_exists("3",$_times[$key])){
                  $tempTime["3"] = 0;
              }
              if (!key_exists("4",$_times[$key])){
                  $tempTime["4"] = 0;
              }
              $_times[$key] = $tempTime;
          }
          //print_r($_times);
          $value['times'] = $_times;
          */
            $value['room_type_str'] = $value['room_type'] == 0 ?'固定': '临时';
            unset($value['edifice']);
            unset($value['edifice_id']);
            unset($value['images']);
            unset($value['status_text']);
            unset($value['Status']);
            unset($value['room_type_text']);
            unset($value['facilities']);
            unset($value['content']);
            unset($value['weigh']);
            unset($value['manager_id']);
            unset($value['status']);
            unset($value['is_verify']);
            $value['site'] = $value['title'];
           
        }
        return $this->success("查询成功", $rooms);
    }



    // 预定
    public function add()
    {
        $user = $this->auth->getUser();
        $input = input();
        $input['user_id'] = $this->auth->id;
        $input['nickname'] = $user['nickname'];
        $info = RoomOrderModel::saveData($input) ;
        if ($info['status'] == 1) {
            $this->success($info['msg']);
        } else {
            $this->error(  $info['msg']);
        }
    }


    /**
     * 房间详情
     * @return [type] [description]
     */
    public function info()
    {
        $year  = $this->request->param('year', date('Y'));
        $month = $this->request->param('month', date('m'));
        $day   = $this->request->param('day', date('d'));
        $id    = $this->request->param('id');
        $room  =RoomModel::where('id', $id)->find();
        // 查询房间预订情况
        $orders      = RoomOrderModel::where('room_id', $room['id'])->where('status', 'neq', -1)->where('starttime', '>', time() - 86400)->field('starttime,endtime')->select();
        $order_times = [];
        foreach ($orders as $order) {
            //每小时分成四份显示
            $step = 15 * 60;
            for ($i = $order['starttime']; $i < $order['endtime']; $i = $i + $step) {
                //获取时间刻钟
                $h = strtotime(date('Y-m-d H:00:00', $i));
                for ($j = 3; $j >= 0; $j--) {
                    if ($i - $h >= $j * $step) {
                        $order_times[] = date('n-j-G', $i) . '-' . $j;
                        break;
                    }
                }
            }
        }
        // 获取开放时间
        $times  = RoomTimesModel::field('time')->where('room_id', $room['id'])->select();
        $_times = [];
        foreach ($times as $time) {
            for ($i = 0; $i < 4; $i++) {
                $_times[$time['time']][] = !in_array($month . '-' . $day . '-' . $time['time'] . '-' . $i, $order_times);
            }
        }
        $room['times'] = $_times;

        return $this->success("查询成功", $room);
    }

    // 详情
    public function detail()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('请填写会议室');
        }
        $input = input();
        $input['user_id'] = $this->auth->id;
        $day = input('date') ? input('date') : date("Y-m-d");
        $info = RoomModel::detail($input,$day);
        $this->success('成功', $info);
    }

}
